<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- //卡片试图 -->
    <el-card>
      <el-row>
        <el-col :span="8">
          <el-input @clear="queryOrder" :clearable="true" placeholder="请输入内容" v-model="queryInfo.query">
            <el-button @click="queryOrder" slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
      </el-row>
      <!-- 订单表格视图 -->
      <el-table border stripe :data="orderList">
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="订单编号" prop="order_number"></el-table-column>
        <el-table-column label="订单价格" width="100px" prop="order_price"></el-table-column>
        <el-table-column label="是否付款" prop="order_pay" width="130px">
          <template slot-scope="scope">
            <el-button v-if="scope.row.order_pay === '1'" type="success" plain>已付款</el-button>
            <el-button v-else type="danger" plain>未付款</el-button>
          </template>
        </el-table-column>
        <el-table-column label="是否发货" prop="is_send" width="100px"></el-table-column>
        <el-table-column label="下单时间" width="200px">
          <template slot-scope="scope">
            {{scope.row.update_time|getDate}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="170px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit" @click="editAddress"></el-button>
            <el-button type="success" size="mini" icon="el-icon-location" @click="logistics(scope.row.order_id)">
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- //页脚视图 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum" :page-sizes="[3, 5, 10, 20]" :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <!-- 修改地址 -->
    <el-dialog title="修改地址" :visible.sync="addressVisible" width="40%">
      <el-form :model="editFormAddress" :rules="editFormAddressRules" ref="editFormAddressRulesRef" label-width="100px">
        <el-form-item label="省市区/县" prop="city">
          <el-cascader v-model="editFormAddress.city" :options="cityData" :props="{ expandTrigger: 'hover' }"
            @change="handleChange">
          </el-cascader>
        </el-form-item>
        <el-form-item label="省市区/县" prop="detailAddress">
          <el-input v-model="editFormAddress.detailAddress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 物流信息 -->
    <el-dialog title="物流信息" :visible.sync="logisticsVisible" width="40%">
      <el-timeline :reverse="true">
        <el-timeline-item v-for="(activity, index) in logisticsTimeLine" :key="index" :timestamp="activity.ftime">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
      <span slot="footer" class="dialog-footer">
        <el-button @click="logisticsVisible = false">取 消</el-button>
        <el-button type="primary" @click="logisticsVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import cityData from './citydata.js'
export default {
  data () {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      // 数据总条数
      total: 0,
      // 订单表格数据源
      orderList: [],
      // 显示修改地址的dialog
      addressVisible: false,
      // 修改地址的数据源
      editFormAddress: {
        city: [],
        detailAddress: ''
      },
      editFormAddressRules: {
        city: [{ required: true, message: '请选择城市' }],
        detailAddress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      cityData,
      logisticsVisible: false,
      logisticsShow: [],
      logisticsTimeLine: []
    }
  },
  created () {
    this.getOrderList()
  },
  methods: {
    // 获取订单数据
    async getOrderList () {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('订单数据获取失败')
      }
      this.total = res.data.total
      this.orderList = res.data.goods
      console.log(res)
    },
    // 改变页脚获取数据的大小
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    // 改变当前页码
    handleCurrentChange (newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    queryOrder () {
      console.log(this.queryInfo.query)
      this.getOrderList()
    },
    // 修改地址
    editAddress () {
      this.addressVisible = true
    },
    // 修该联动，触发
    handleChange () {},
    // 显示物流
    async logistics (id) {
      this.logisticsVisible = true
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) return this.$message.error('物流获取失败')
      console.log(res.data)
      this.logisticsTimeLine = res.data
    }
  }
}
</script>
<style lang="less" scoped>
</style>
